using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnInFront : MonoBehaviour
{
    public GameObject prefabToSpawn;     // 要生成的预制体
    public float minSpawnInterval = 3f;  // 最小生成间隔
    public float maxSpawnInterval = 6f;  // 最大生成间隔

    private float spawnInterval;
    private float timer;

    void Start()
    {
        spawnInterval = Random.Range(minSpawnInterval, maxSpawnInterval);
    }

    void Update()
    {
        timer += Time.deltaTime;

        if (timer >= spawnInterval)
        {
            timer = 0f;

            // 50% 概率生成
            if (Random.value < 0.5f)
            {
                Spawn();
            }

            // 每次生成后重新设置 spawnInterval
            spawnInterval = Random.Range(minSpawnInterval, maxSpawnInterval);
        }
    }

    void Spawn()
    {
        Vector3 spawnPosition = transform.position;
        spawnPosition.y = 0f; // 直接设置 Y 为 0
        Quaternion rotation = Quaternion.Euler(-90f, 0f, 0f);

        Instantiate(prefabToSpawn, spawnPosition, rotation);
    }
}
